
AllSpells = {}

local RetrievedData = false
local SelectedPreset = nil
local SelectedClass = nil
local Char_Presets = nil
local Char_SetOptions = nil

local UsableSpells = {{},{},{},{},{}}

local AllClasses = {
	"Man At Arms",
	[[With unmatched strength and speed,
	the Man at Arms is great for putting
	out quick bursts of damage.]],
	"Ranger",
	[[Rangers use their ranged weapons
	and specialized shots to take down
	targets.]],
	"Rogue",
	[[Rogues make use of their agility 
	and mastery of shadow magic to gut 
	their foes.]],
	"Mage",
	[[Annihilates targets with deadly 
	elemental energies. Mages can employ 
	the use of fire, frost, and blood
	magic.
	]],
	--"Bear",
	--[[It's a fuckin' bear, man.]]
	"Paladin",
	[[A defensive warrior who channels 
	light to protect his allies and smite
	enemies.]],
}

ClassDefaultSpells = {
	["Man At Arms"] = {REGEN_RAGE, {51312, 1100, 60, 81240, 50}, "class_maa"},
	["Ranger"] = {REGEN_FOCUS, {5101, 13403, 11102, 15403, 15405}, "class_ranger"},
	["Rogue"] = {REGEN_ENERGY, {51313, 22104, 22105, 41113, 21104}, "class_rogue"},
	["Paladin"] = {REGEN_HOLYPOWER, {10912, 19191, 21121, 21120, 48442}, "class_paladin"},
}

net.Receive("SendPlayerInventory", function()
	local data = net.ReadTable()
	
	OwnedItems = util.JSONToTable(data[1])
	OwnedSpells = util.JSONToTable(data[2])
	OwnedCurrency = util.JSONToTable(data[4])
	Char_Presets = util.JSONToTable(data[5])
	Char_SetOptions = util.JSONToTable(data[6])
	
	if( Char_Presets == nil )then
		Char_Presets = {}
	end
	
	timer.Simple(0.5, function()
		RetrievedData = true
	end)
end)

net.Receive("SendAllSpells", function() 
	print("received all spells")
	AllSpells = net.ReadTable()
end)

net.Receive("OpenPresetSelect", function()
	OpenPresetSelection()
end)

function OpenPresetSelection()
	if(!MenuOpen)then
		if(!RetrievedData)then
			net.Start("RetrievePlayerInventory")
			net.SendToServer()
			surface.PlaySound("devin/blackbetty/menu/select_target.wav")
			local RetrievingMenu = vgui.Create("DFrame")
				RetrievingMenu:SetTitle( "..." )
				RetrievingMenu:SetPos( ScrW()/2, ScrH()/2 )
				RetrievingMenu:SetSize( 200, 100 )
				RetrievingMenu:SetDeleteOnClose(true)
				RetrievingMenu:ShowCloseButton(false)
				RetrievingMenu.Think = function() 
					if(RetrievedData)then
						RetrievingMenu:Remove()
						OpenPresetSelection()
					end
				end
			
			local RetrievingMenuLabel = vgui.Create( "DLabel", RetrievingMenu)
				RetrievingMenuLabel:SetText( "Retrieving player data." )
				RetrievingMenuLabel:Center()
				RetrievingMenuLabel:SizeToContents()
			
		else
			surface.PlaySound("devin/blackbetty/menu/spellbook_open.wav")
			MenuOpen = true
			optionsMenu = vgui.Create("DFrame")
				optionsMenu:SetTitle( "Preset selection." )
				optionsMenu:SetSize( 500, 500 )
				optionsMenu:SetDeleteOnClose(true)
				optionsMenu:ShowCloseButton(true)
				optionsMenu:Center()
				optionsMenu:SetMouseInputEnabled(true)
				optionsMenu:MakePopup()
				optionsMenu.Close = function() 
					MenuOpen = false 
					optionsMenu:Remove() 
					RetrievedData = false
					surface.PlaySound("devin/blackbetty/menu/spellbook_close.wav") 
			end
			
			CharPresets = vgui.Create("DImage", optionsMenu)
				CharPresets:SetPos( 5, 25 )
				CharPresets:SetSize( 490, 470 )
				CharPresets:SetImage("devin/blackbetty/spellbook/background.png")
				
			local DBanner = vgui.Create( "DLabel", optionsMenu )
				DBanner:SetSize( 300, 100 )
				DBanner:SetPos( 150, 50 )
				DBanner:SetText( "RPG:DM" )
				DBanner:SetTextColor(Color(125, 0, 0, 255))
				DBanner:SetFont( "DamageText" )
			
			local DLabel = vgui.Create( "DLabel", optionsMenu )
				DLabel:SetSize( 300, 100 )
				DLabel:SetPos( 130, 120 )
				DLabel:SetText( [[Select the preset to be used.]] )
				DLabel:SetTextColor(Color(0, 0, 0, 255))
				DLabel:SetFont( "HUDText_Quests" )
			
			PresetList = vgui.Create( "DComboBox", optionsMenu )
			PresetList:SetPos( 145, 200 )
			PresetList:SetSize( 200, 40 )
			PresetList:SetValue( "No presets." )
			if(Char_Presets != nil)then
					for i = 1, #Char_Presets, 1 do
						print(#Char_Presets)
						if(Char_Presets[i][1] != "" or Char_Presets[i][1] != nil)then
							PresetList:AddChoice(Char_Presets[i][1])
						end
					end
				
				if(Char_Presets[1] != nil)then
					PresetList:SetValue( "Choose a preset." )
				else
					PresetList:SetValue( "No presets." )
				end
			end
			
			PresetList.OnSelect = function( panel, index, value, data )
				SelectedPreset = index
				net.Start("UpdatePlayerOptions")
				Char_SetOptions.SelectedPreset = index
				net.WriteString(util.TableToJSON(Char_SetOptions))
				net.SendToServer()
				MakePresetSelectIcons(optionsMenu, index)
			end
			
			if(!Char_SetOptions.SelectedPreset and Char_Presets[1] != nil)then
				PresetList:ChooseOptionID(1)
			elseif(Char_SetOptions.SelectedPreset and Char_Presets[Char_SetOptions.SelectedPreset] != nil)then
				PresetList:ChooseOptionID(Char_SetOptions.SelectedPreset)
			end
			
			RemovePreset = vgui.Create( "DButton", optionsMenu)
			RemovePreset:SetPos( 275, 330 )
			RemovePreset:SetText( "Remove Preset" )
			RemovePreset:SetSize( 100, 20 )
			RemovePreset.DoClick = function()
				if(#Char_Presets > 0)then
					surface.PlaySound("physics/plaster/ceilingtile_break2.wav")
					if(PresetList != nil)then
						PresetList:Remove()
					end
					RemovePresetContent()
					table.remove(Char_Presets, SelectedPreset)
					net.Start("UpdatePlayerPresets")
					net.WriteString(util.TableToJSON(Char_Presets))
					net.SendToServer()
					PresetList = vgui.Create( "DComboBox", optionsMenu )
					PresetList:SetPos( 145, 200 )
					PresetList:SetSize( 200, 40 )
					PresetList:SetValue( "No presets." )
					for i = 1, #Char_Presets, 1 do
						print(#Char_Presets)
						if(Char_Presets[i][1] != "" or Char_Presets[i][1] != nil)then
							PresetList:AddChoice(Char_Presets[i][1])
						end
					end
					if(Char_Presets[1] != nil)then
						PresetList:SetValue( "Choose a preset." )
					else
						PresetList:SetValue( "No presets." )
					end
					PresetList.OnSelect = function( panel, index, value, data )
						SelectedPreset = index
						MakePresetSelectIcons(optionsMenu, index)
					end
					if(!Char_SetOptions.SelectedPreset and Char_Presets[1] != nil)then
						PresetList:ChooseOptionID(1)
					elseif(Char_SetOptions.SelectedPreset and Char_Presets[Char_SetOptions.SelectedPreset] != nil)then
						PresetList:ChooseOptionID(Char_SetOptions.SelectedPreset)
					end
					if(#Char_Presets > 0)then
						PresetList:ChooseOptionID( #Char_Presets )
					else
						RemovePresetElements()
					end
				end
			end
			local CreatePreset = vgui.Create( "DButton", optionsMenu )
			CreatePreset:SetPos( 120, 330 )
			CreatePreset:SetText( "Create Preset" )
			CreatePreset:SetSize( 100, 20 )
			CreatePreset.DoClick = function()
				surface.PlaySound("devin/blackbetty/menu/select_target.wav")
				
				local MakeCoverUp = vgui.Create( "DPanel", optionsMenu )
				MakeCoverUp:SetPos( 5, 25 )
				MakeCoverUp:SetPos( 5, 25 )
				MakeCoverUp:SetSize( 490, 470 )
				MakeCoverUp:SetBackgroundColor(Color(0, 0, 0, 235))
				
				local MakePresetWindowUnder = vgui.Create( "DPanel", optionsMenu )
				MakePresetWindowUnder:SetPos( 0, 256 )
				MakePresetWindowUnder:SetSize( 490, 160 )
				MakePresetWindowUnder:SetBackgroundColor(Color(0, 0, 0, 110))
				
				local MakePresetPanel = vgui.Create( "DPanel", MakePresetWindowUnder )
				MakePresetPanel:SetPos( 10, 30 )
				MakePresetPanel:SetSize( 245, 55 )
					
				local SmallTextBkg = vgui.Create( "DPanel", MakePresetWindowUnder )
				SmallTextBkg:SetPos( 285, 30 )
				SmallTextBkg:SetSize( 150, 16 )
					
				local ChooseClassLabel = vgui.Create( "DLabel", MakePresetWindowUnder )
				ChooseClassLabel:SetPos( 300, 30 )
				ChooseClassLabel:SetText( "Select your preset's class:" )
				ChooseClassLabel:SizeToContents()
				ChooseClassLabel:SetDark( 1 )
					
				local SmallTextBkg2 = vgui.Create( "DPanel", MakePresetWindowUnder )
				SmallTextBkg2:SetPos( 285, 110 )
				SmallTextBkg2:SetSize( 150, 16 )
					
				local ChoosePresetNameLabel = vgui.Create( "DLabel", MakePresetWindowUnder )
				ChoosePresetNameLabel:SetPos( 300, 110 )
				ChoosePresetNameLabel:SetText( "Select your preset's name:" )
				ChoosePresetNameLabel:SizeToContents()
				ChoosePresetNameLabel:SetDark( 1 )
					
				local TextEntry = vgui.Create( "DTextEntry", MakePresetWindowUnder )              
				TextEntry:SetPos( 285, 130 )                
				TextEntry:SetSize( 150, 16 )          
				TextEntry:SetText( "Preset" )
					
				local OutOfCreatePreset = vgui.Create( "DButton", MakePresetWindowUnder )
				OutOfCreatePreset:SetPos( 10, 120 )
				OutOfCreatePreset:SetText( "Cancel" )
				OutOfCreatePreset:SetSize( 100, 20 )
				OutOfCreatePreset.DoClick = function()
					MakePresetWindowUnder:Remove()
					MakeCoverUp:Remove()
					surface.PlaySound("devin/blackbetty/menu/select_target.wav")
				end
					
			local DoCreatePreset = vgui.Create( "DButton", MakePresetWindowUnder )
			DoCreatePreset:SetPos( 140, 120 )
			DoCreatePreset:SetText( "Create" )
			DoCreatePreset:SetSize( 100, 20 )
			DoCreatePreset.DoClick = function()
				surface.PlaySound("devin/blackbetty/menu/select_target.wav")
				if(SelectedClass != "")then
					print(SelectedClass)
					local temptable = table.Add(Char_Presets, {{TextEntry:GetValue(), SelectedClass, ClassDefaultSpells[SelectedClass][2], {1, 0, 150}, {}, ClassDefaultSpells[SelectedClass][1], ClassDefaultSpells[SelectedClass][3]}})
					net.Start("UpdatePlayerPresets")
					net.WriteString(util.TableToJSON(Char_Presets))
					net.SendToServer()
					MakePresetWindowUnder:Remove()
					MakeCoverUp:Remove()
					PresetList = vgui.Create( "DComboBox", optionsMenu )
					PresetList:SetPos( 145, 200 )
					PresetList:SetSize( 200, 40 )
					PresetList:SetValue( "No presets." )
					for i = 1, #Char_Presets, 1 do
						print(#Char_Presets)
						if(Char_Presets[i][1] != "" or Char_Presets[i][1] != nil)then
							PresetList:AddChoice(Char_Presets[i][1])
						end
					end
					if(Char_Presets[1] != nil)then
						PresetList:SetValue( "Choose a preset." )
					else
						PresetList:SetValue( "No presets." )
					end
					PresetList.OnSelect = function( panel, index, value, data )
						SelectedPreset = index
						MakePresetSelectIcons(optionsMenu, index)
					end
					if(!Char_SetOptions.SelectedPreset and Char_Presets[1] != nil)then
						PresetList:ChooseOptionID(1)
					elseif(Char_SetOptions.SelectedPreset and Char_Presets[Char_SetOptions.SelectedPreset] != nil)then
						PresetList:ChooseOptionID(Char_SetOptions.SelectedPreset)
					end
					PresetList:ChooseOptionID( #Char_Presets )
				else
					surface.PlaySound("buttons/button10.wav")
					end
				end
				CreateClassDescText = vgui.Create( "DLabel", MakePresetPanel )
				CreateClassDescText:SetPos( 8, 5 )
				CreateClassDescText:SetSize( 245, 200 )
				CreateClassDescText:SetText( "Select a class ->" )
				CreateClassDescText:SizeToContentsY()
				CreateClassDescText:SetDark( 1 )
					
				local ChooseClassList = vgui.Create( "DComboBox", MakePresetWindowUnder )
				ChooseClassList:SetPos( 285, 50 )
				ChooseClassList:SetSize( 150, 16 )
				for i = 1, #AllClasses, 2 do
					if(AllClasses[i] != "" or AllClasses[i] != nil)then
						ChooseClassList:AddChoice(AllClasses[i])
					end
				end
				ChooseClassList.OnSelect = function( panel, index, value, data )
					surface.PlaySound("devin/blackbetty/menu/select_target.wav")
					if(CreateClassDescText != nil)then
						CreateClassDescText:Remove()
					end
					
					CreateClassDescText = vgui.Create( "DLabel", MakePresetPanel )
					CreateClassDescText:SetPos( 8, 5 )
					CreateClassDescText:SetSize( 245, 200 )
					CreateClassDescText:SetText( AllClasses[2*index] )
					CreateClassDescText:SizeToContentsY()
					CreateClassDescText:SetDark( 1 )
						
					SelectedClass = AllClasses[(index*2)-1]
				end

			end
			
			local SpawnWithPreset = vgui.Create( "DButton", optionsMenu )
			SpawnWithPreset:SetPos( 150, 400 )
			SpawnWithPreset:SetText( "[TEMP]Spawn with Preset" )
			SpawnWithPreset:SetSize( 200, 20 )
			SpawnWithPreset.DoClick = function()
				if(#Char_Presets > 0)then
					local preset_info = {}
					preset_info[1] = Char_Presets[SelectedPreset][3]
					preset_info[2] = ClassDefaultSpells[Char_Presets[SelectedPreset][2]][1]
					preset_info[3] = ClassDefaultSpells[Char_Presets[SelectedPreset][2]][3]
					preset_info[4] = Char_Presets[SelectedPreset][1]
					
					PrintTable(preset_info)
					-- for i = 1, #ClassFillers, 1 do
						-- if(CurrentClassName == ClassFillers[i][1])then
							-- table.insert(preset_info[1], ClassFillers[i][2])
							-- table.insert(preset_info[1], ClassFillers[i][3])
						-- end
					-- end
					net.Start("SpawnWithPreset")
					net.WriteTable(preset_info)
					net.SendToServer()
					optionsMenu:Close()
				end
			end
		end
	else
		optionsMenu:Close()
	end
end


function RemovePresetElements()

	if(ClassLabel != nil)then
		ClassLabel:Remove()
	end
	
	if(SpellBox1 != nil)then
		SpellBox1:Remove()
	end
	
	if(SpellBox2 != nil)then
		SpellBox2:Remove()
	end
	
	if(SpellBox3 != nil)then
		SpellBox3:Remove()
	end
	
	if(SpellBox4 != nil)then
		SpellBox4:Remove()
	end
	
	if(SpellBox5 != nil)then
		SpellBox5:Remove()
	end
	
end

function MakePresetSelectIcons(parent, index)
	PrintTable(Char_Presets)
	local xoffset = 24
	local ypos = 270
	SpellBoxes = {}
	
	if(SpellBox1 != nil)then
		SpellBox1:Remove()
	end
	
	if(ClassLabel != nil)then
		ClassLabel:Remove()
	end
	
	ClassLabel = vgui.Create( "DLabel", optionsMenu )
		ClassLabel:SetSize( 300, 100 )
		ClassLabel:SetPos( 150, 204 )
		ClassLabel:SetText( "Level "..(Char_Presets[index][4][1]).." "..Char_Presets[index][2].."" )
		ClassLabel:SetTextColor(Color(25, 25, 205, 255))
		ClassLabel:SetFont( "HUDText_Vendor" )
	
	SpellBox1 = vgui.Create( "DImageButton", parent )
		SpellBox1:SetPos( 34+xoffset, ypos )
		SpellBox1:SetSize( 50, 50 )
		SpellBox1:SetImage("devin/blackbetty/hudicons/plus.png")
	SpellBoxes[1] = SpellBox1
	
	if(SpellBox2 != nil)then
		SpellBox2:Remove()
	end
	
	SpellBox2 = vgui.Create( "DImageButton", parent )
		SpellBox2:SetPos( 118+xoffset, ypos )
		SpellBox2:SetSize( 50, 50 )
		SpellBox2:SetImage("devin/blackbetty/hudicons/plus.png")
	SpellBoxes[2] = SpellBox2
		
	if(SpellBox3 != nil)then
		SpellBox3:Remove()
	end
	
	SpellBox3 = vgui.Create( "DImageButton", parent )
		SpellBox3:SetPos( 200+xoffset, ypos )
		SpellBox3:SetSize( 50, 50 )
		SpellBox3:SetImage("devin/blackbetty/hudicons/plus.png")
	SpellBoxes[3] = SpellBox3
	
	if(SpellBox4 != nil)then
		SpellBox4:Remove()
	end
	
	SpellBox4 = vgui.Create( "DImageButton", parent )
		SpellBox4:SetPos( 284+xoffset, ypos )
		SpellBox4:SetSize( 50, 50 )
		SpellBox4:SetImage("devin/blackbetty/hudicons/plus.png")
	SpellBoxes[4] = SpellBox4
		
	if(SpellBox5 != nil)then
		SpellBox5:Remove()
	end
	
	SpellBox5 = vgui.Create( "DImageButton", parent )
		SpellBox5:SetPos( 368+xoffset, ypos )
		SpellBox5:SetSize( 50, 50 )
		SpellBox5:SetImage("devin/blackbetty/hudicons/plus.png")
	SpellBoxes[5] = SpellBox5
		
	for k, v in pairs(SpellBoxes)do
		for i, q in pairs(AllSpells)do
			if(AllSpells[i][6] == Char_Presets[index][3][k])then
				v:SetImage(AllSpells[i][2])
				SpellBoxes[k].OnCursorEntered = function()
				firsttooltipPanel = vgui.Create("DImage", parent)
					firsttooltipPanel:SetVisible(true)
					firsttooltipPanel:SetImage("devin/blackbetty/tooltip/window.png")
					firsttooltipPanel:SetSize(165, 108)
					firsttooltipPanel:SetPos((64*k)-48, 320)
				firsttooltipImage = vgui.Create("DImage", firsttooltipPanel)
					firsttooltipImage:SetVisible(true)
					firsttooltipImage:SetImage(AllSpells[i][2])
					firsttooltipImage:SetSize(48, 48)
					firsttooltipImage:SetPos(4, 4)
				firsttooltipTitle = vgui.Create("DLabel", firsttooltipPanel)
					firsttooltipTitle:SetVisible(true)
					firsttooltipTitle:SetText(AllSpells[i][1])
					firsttooltipTitle:SetSize(128, 16)
					firsttooltipTitle:SetPos(64, 8)
				firsttooltipTitle = vgui.Create("DLabel", firsttooltipPanel)
					firsttooltipTitle:SetVisible(true)
					firsttooltipTitle:SetText("Range: "..(math.Round(AllSpells[i][4]/16/3)).." yds." )
					firsttooltipTitle:SetSize(128, 32)
					firsttooltipTitle:SetPos(64, 16)
				firsttooltipDesc = vgui.Create("DLabel", firsttooltipPanel)
					firsttooltipDesc:SetVisible(true)
					firsttooltipDesc:SetText(AllSpells[i][3])
					firsttooltipDesc:SetSize(256, 64)
					firsttooltipDesc:SetPos(12, 48)
				end
				SpellBoxes[k].OnCursorExited = function()
					firsttooltipPanel:Remove()
				end
				break
			end
		end
	end

	
	local ConfirmButton = vgui.Create( "DButton", optionsMenu )
		ConfirmButton:SetSize( 200, 50 )
		ConfirmButton:SetPos( 150, 430 )
		ConfirmButton:SetText( "Upgrades("..#Char_Presets[index][5]..")" )
		if(#Char_Presets[index][5]>0)then
			ConfirmButton.DoClick = function()
				CreateUpgradeMenu(index, optionsMenu)
				surface.PlaySound("devin/blackbetty/menu/spellbook_open.wav")
			end
		else
			ConfirmButton:SetDisabled(true)
		end
end

function CreateUpgradeMenu(index, parent)
	upgradeMenu = vgui.Create("DPanel", parent)
	upgradeMenu:SetPos( 5, 25 )
	upgradeMenu:SetSize( 490, 470 )
	upgradeMenu:SetMouseInputEnabled(true)
	upgradeMenu:SetBackgroundColor(Color(100, 100, 100, 255))
	upgradeMenu.Close = function() 
		upgradeMenu:Remove() 
		surface.PlaySound("devin/blackbetty/menu/spellbook_close.wav") 
	end
	
	scrollBkg = vgui.Create("DImage", upgradeMenu)
	scrollBkg:SetPos( 0, 0 )
	scrollBkg:SetSize( 490, 470 )
	scrollBkg:SetImage("devin/blackbetty/spellbook/background.png")

	
	local upgradeList = vgui.Create( "DIconLayout", parent )
	upgradeList:SetSize( 470, 200 )
	upgradeList:SetPos( 20, 40 )
	
	upgradeList:SetSpaceY( 15 )
	
	for i = 1, 3, 1 do
		local upgradeData = Char_Presets[index][5][i]
		local upgrade = upgradeList:Add( "DPanel" )
		upgrade:SetBackgroundColor(Color(45*i, 45*i, 45*i, 255-(50*i)))
		upgrade:SetSize( 450, 120 )
		upgrade:SetParent(upgradeList)
		
		local DLabel = vgui.Create( "DLabel", upgrade )
		DLabel:SetPos( 0, 0 )
		DLabel:SetSize( 440, 50 )
		DLabel:SetText( "No Upgrade" )
		DLabel:SetFont( "HUDText_Quests" )
		DLabel:SetTextColor( Color( 30, 30, 30, 255-(50*i) ) )
		DLabel:Center()
			
		local upgradecontent = vgui.Create( "DPanel", upgrade )
		upgradecontent:SetPos( 150, 0 )
		upgradecontent:SetSize( 300, 120 )
		upgradecontent:SetBackgroundColor(Color(45*i, 45*i, 45*i, 255-(50*i)))
		
		if(upgradeData != nil)then
			local upgradeType = upgradeData[3];
			if(upgradeType == 1)then
				DLabel:SetText("Upgrade Spell "..upgradeData[2].."")
				DLabel:SetTextColor( Color( 55, 255, 55, 255-(50*i) ) )
				
				local upgSpell1 = GetSpellByID(upgradeData[1][1])
				local upgIcon1 = vgui.Create( "DImageButton", upgradecontent )
				upgIcon1:SetPos( 40, 30 )
				upgIcon1:SetSize( 64, 64 )
				upgIcon1:SetImage( upgSpell1[2] )
				if(i != 1)then
					upgIcon1:SetDisabled(true)
				end
				--upgIcon1:SetImageColor(Color(255, 255, 255, 255-(50*(i-1))))
				upgIcon1.DoClick = function()
					net.Start("ChooseSpellUpgrade")
					net.WriteTable({index, upgradeData[2], upgradeData[1][1]})
					net.SendToServer()
					optionsMenu:Close()
				end
				upgIcon1.OnCursorEntered = function()
					upg1t_Panel = vgui.Create("DImage", upgradecontent)
					upg1t_Panel:SetVisible(true)
					upg1t_Panel:SetImage("devin/blackbetty/tooltip/window.png")
					upg1t_Panel:SetSize(165, 108)
					upg1t_Panel:SetPos(40, 5)
					
					upg1t_Image = vgui.Create("DImage", upg1t_Panel)
					upg1t_Image:SetVisible(true)
					upg1t_Image:SetImage(upgSpell1[2])
					upg1t_Image:SetSize(48, 48)
					upg1t_Image:SetPos(4, 4)
					
					upg1t_Title = vgui.Create("DLabel", upg1t_Panel)
					upg1t_Title:SetVisible(true)
					upg1t_Title:SetText(upgSpell1[1])
					upg1t_Title:SetSize(128, 16)
					upg1t_Title:SetPos(64, 8)
					
					upg1t_Title = vgui.Create("DLabel", upg1t_Panel)
					upg1t_Title:SetVisible(true)
					upg1t_Title:SetText("Range: "..(math.Round(upgSpell1[4]/16/3)).." yds." )
					upg1t_Title:SetSize(128, 32)
					upg1t_Title:SetPos(64, 16)
					
					upg1t_Desc = vgui.Create("DLabel", upg1t_Panel)
					upg1t_Desc:SetVisible(true)
					upg1t_Desc:SetText(upgSpell1[3])
					upg1t_Desc:SetSize(256, 64)
					upg1t_Desc:SetPos(12, 48)
				end
				
				upgIcon1.OnCursorExited = function()
					upg1t_Panel:Remove()
				end
				
				local upgSpell2 = GetSpellByID(upgradeData[1][2])
				local upgIcon2 = vgui.Create( "DImageButton", upgradecontent )
				upgIcon2:SetPos( 190, 30 )
				upgIcon2:SetSize( 64, 64 )
				upgIcon2:SetImage( upgSpell2[2] )
				if(i != 1)then
					upgIcon2:SetDisabled(true)
				end
				--upgIcon2:SetImageColor(Color(255, 255, 255, 255-(50*(i-1))))
				upgIcon2.DoClick = function()
					net.Start("ChooseSpellUpgrade")
					net.WriteTable({index, upgradeData[2], upgradeData[1][2]})
					net.SendToServer()
					optionsMenu:Close()
				end
				upgIcon2.OnCursorEntered = function()
					upg2t_Panel = vgui.Create("DImage", upgradecontent)
					upg2t_Panel:SetVisible(true)
					upg2t_Panel:SetImage("devin/blackbetty/tooltip/window.png")
					upg2t_Panel:SetSize(165, 108)
					upg2t_Panel:SetPos(120, 5)
					
					upg2t_Image = vgui.Create("DImage", upg2t_Panel)
					upg2t_Image:SetVisible(true)
					upg2t_Image:SetImage(upgSpell2[2])
					upg2t_Image:SetSize(48, 48)
					upg2t_Image:SetPos(4, 4)
					
					upg2t_Title = vgui.Create("DLabel", upg2t_Panel)
					upg2t_Title:SetVisible(true)
					upg2t_Title:SetText(upgSpell2[1])
					upg2t_Title:SetSize(128, 16)
					upg2t_Title:SetPos(64, 8)
					
					upg2t_Title = vgui.Create("DLabel", upg2t_Panel)
					upg2t_Title:SetVisible(true)
					upg2t_Title:SetText("Range: "..(math.Round(upgSpell2[4]/16/3)).." yds." )
					upg2t_Title:SetSize(128, 32)
					upg2t_Title:SetPos(64, 16)
					
					upg2t_Desc = vgui.Create("DLabel", upg2t_Panel)
					upg2t_Desc:SetVisible(true)
					upg2t_Desc:SetText(upgSpell2[3])
					upg2t_Desc:SetSize(256, 64)
					upg2t_Desc:SetPos(12, 48)
				end
				
				upgIcon2.OnCursorExited = function()
					upg2t_Panel:Remove()
				end
			end
		end
	end
	
	local cancelPanel = vgui.Create("DPanel", upgradeMenu)
	cancelPanel:SetPos( 0,  430)
	cancelPanel:SetSize( 490, 50 )
	cancelPanel:SetBackgroundColor(Color(100, 100, 100, 255))
	
	local CloseButton = vgui.Create( "DButton", optionsMenu )
	CloseButton:SetSize( 200, 20 )
	CloseButton:SetPos( 150, 465 )
	CloseButton:SetText( "Close" )
	CloseButton.DoClick = function()
		upgradeMenu:Close()
		upgradeList:Remove()
		CloseButton:Remove()
	end
end